<?php
session_start();		//启动session
if(!isset($_SESSION['username']) || empty($_SESSION['username'])){
	header("location:/demo/login.php");
	eixt();
}
//链接数据库
$link= mysqli_connect("127.0.0.1", "root", "", "demo");

//接收数据
//var_dump($_POST);
if(isset($_POST['username']) && $_POST['username']!=""){
	$username=$_POST['username'];
	$age=$_POST['age'];
	$sex=$_POST['sex'];

	$imagename=$_FILES['image']['name'];		//原文件名
	$tmp_name=$_FILES['image']['tmp_name'];		//临时路径


	//把图片从临时路径移动到upload
	$result = move_uploaded_file($tmp_name, "./upload/".$imagename);

	//图片在服务器中的路径
	$image="/upload/".$imagename;

	//准备sql
	$sql="insert into users values(null,'$username',$age,$sex,'$image')";
	// var_dump($sql);exit();

	
	//执行添加
	$result= mysqli_query($link, $sql);
	if($result){
		header("location:/demo/list.php");
	}
}

?>

<html>
	<head>
		<title></title>
	</head>
	<body>
		<h1>添加</h1>

		<form action="./add.php" method="post"  enctype="multipart/form-data">
			
			<div>
				<label for="">用户名</label>
				<input type="text" name="username" />
			</div>
			<div id="">
				<label for="">年龄</label>
				<input type="text" name="age" />
			</div>
			<div>
				<label for="">性别</label>
				<input type="radio" name="sex" checked value="1" />男
				<input type="radio" name="sex" value="2" />女
			</div>
			<div>
				<label>图片</label>
				<input type="file" name="image" />
			</div>
			<button>提交</button>
		</form>		
	</body>
</html>


